<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginFormRef">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              :prefix-icon="Lock"
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" class="login_btn" type="primary" block @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive,ref } from "vue";
import useUserStore from "@/store/modules/user";
import {  ElNotification } from "element-plus";
import router from "@/router";
import { getTime } from "@/utils/time";
import { useRoute } from "vue-router";

const userStore = useUserStore();
let loading = ref(false);
let loginForm = reactive({
  username: "admin",
  password: "111111",
});
let route = useRoute()
const rules = {
  username: [
    {trigger:"change",validator:validatorUserName}
],
  password: [
    { required: true, min: 6, max: 10, message: "密码长度必须在 6 到 10 个字符", trigger: "change" },
  ],
};
let loginFormRef = ref();
async function login() {
  await loginFormRef.value.validate();
  loading.value = true;
  userStore
    .userLogin(loginForm)
    .then((res) => {
      if (res === "ok") {
        // 需要先导入 ElMessage
        ElNotification({
          message: `欢迎回来`,
          type: "success",
          title: `HI，${getTime()}好`,
        })
        let redirect = route.query.redirect as string 
        console.log('push前',redirect)
        router.push({ path: redirect||'/' });
        console.log('push后',redirect)
      }
    })
    .catch((err) => {
      ElNotification.error(err.message);
    }).finally(() => {
      loading.value = false;
    });
}
function validatorUserName(rule:any, value:any, callback:any) {
  if (value.length < 5 || value.length > 10) { 
    callback(new Error("用户名长度必须在 5 到 10 个字符"));
  } else {
    callback();
  }
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: #fff;
      font-size: 40px;
    }

    h2 {
      color: #fff;
      font-size: 20px;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
